package com.texas_android.app.utils.downLoan;

import android.content.Context;
import android.content.DialogInterface;
import android.view.KeyEvent;
import android.view.View;

import com.google.gson.reflect.TypeToken;
import com.loopj.android.http.TextHttpResponseHandler;
import com.texas_android.app.Appconfig.AppConfig;
import com.texas_android.app.http.Api;
import com.texas_android.app.http.bean.Version;
import com.texas_android.app.utils.AppOperator;
import com.texas_android.app.utils.DialogHelper;
import com.texas_android.app.utils.Logger;

import cz.msebera.android.httpclient.Header;

/**
 * Created by solin on 2018/7/4.
 */

public class CheckUpdateManager {

    private Context mContext;
    private RequestPermissions mCaller;

    public CheckUpdateManager(Context context, boolean showWaitingDialog) {
        this.mContext = context;
    }

    public void checkUpdate() {
        Api.getVersion(new TextHttpResponseHandler() {
            @Override
            public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
                Logger.log("版本更新:" + responseString + ",cur Version:");

            }

            @Override
            public void onSuccess(int statusCode, Header[] headers, String responseString) {
                Logger.log("版本更新:" + responseString);
                try {
                    final Version version = AppOperator.createGson().fromJson(responseString, new TypeToken<Version>() {
                    }.getType());


                    final DialogHelper.UpdateDialog dialog = DialogHelper.openUpdateDialogWithCancel(mContext, version.getContent());
                    dialog.setCanceledOnTouchOutside(false);
                    dialog.setUpdateListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            mCaller.call(version);
                            dialog.dismiss();
                        }
                    });
                    dialog.setCancelListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            dialog.dismiss();
                        }
                    });
                    dialog.setOnKeyListener(new DialogInterface.OnKeyListener() {
                        @Override
                        public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
                            if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
                                return true;
                            }
                            return false;
                        }
                    });
                    dialog.show();

                } catch (Exception e) {
                    if (AppConfig.DEBUG)
                        e.printStackTrace();
                } finally {

                }
            }
        });
    }

    public void setCaller(RequestPermissions caller) {
        this.mCaller = caller;
    }

    public interface RequestPermissions {
        void call(Version version);
    }


}
